package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import dto.BookDTO;
import dto.LogonDTO;
import dto.ReturnBookDTO;
import dto.UserLoanBookDTO;

public class ReturnBookPage extends JDialog {
	private static final long serialVersionUID = 1L;

	private JLabel labelMessage;
	private JPanel panelResults;

	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	private JTextField textFieldReturnerId;
	private JTextField textFieldBorrowername;
	private JTextField textFieldUsername;
	private JTextField textFieldLevel;
	private JButton btnBack = new JButton("Back");

	private LogonDTO logonDTO;
	private ReturnBookDTO returnBookDTO;
	private List<UserLoanBookDTO> loanedBooks;

	public LogonDTO getLogonDTO() {
		return logonDTO;
	}

	public ReturnBookDTO getReturnBookDTO() {

		returnBookDTO.setReturnerId(textFieldReturnerId.getText().trim());
		// returnBookDTO.getBookDTO().setDeweyDecimalNo(
		// textFieldCatalog.getText().trim());

		return returnBookDTO;
	}

	public List<UserLoanBookDTO> getLoanedBooks() {
		return loanedBooks;
	}

	public void setLoanedBooks(List<UserLoanBookDTO> loanedBooks) {
		this.loanedBooks = loanedBooks;
	}

	/**
	 * Create the dialog.
	 */
	public ReturnBookPage(LogonDTO logonDTO, ReturnBookDTO returnBookDTO) {

		this.logonDTO = logonDTO;
		this.returnBookDTO = returnBookDTO;

		setBounds(100, 100, 571, 468);
		getContentPane().setLayout(new BorderLayout());
		{
			JPanel panel = new JPanel();
			getContentPane().add(panel, BorderLayout.NORTH);
			panel.setLayout(new MigLayout("",
					"[][grow][85.00,grow][48.00,grow][][33.00]",
					"[][][][][][][][][][][][]"));
			{
				JLabel lblJaLibrary = new JLabel("J&A Library");
				lblJaLibrary.setFont(new Font("Arial", Font.BOLD, 16));
				lblJaLibrary.setForeground(Color.BLUE);
				panel.add(lblJaLibrary, "cell 0 0 6 1,alignx center");
			}
			{
				JLabel lblBookBorrowing = new JLabel("Book Returning");
				panel.add(lblBookBorrowing, "cell 0 1 6 1,alignx center");
			}
			{
				JLabel label = new JLabel("     ");
				panel.add(label, "cell 0 2");
			}
			{
				JLabel label = new JLabel(
						"                                        ");
				panel.add(label, "cell 1 2 2 1");
			}
			{
				JLabel lblBorrowersName = new JLabel("Returner's Name:");
				panel.add(lblBorrowersName, "cell 0 4,alignx trailing");
			}
			{
				textFieldBorrowername = new JTextField();
				textFieldBorrowername.setEditable(false);
				textFieldBorrowername.setText(returnBookDTO.getReturnerName());
				panel.add(textFieldBorrowername, "cell 1 4 2 1,growx");
				textFieldBorrowername.setColumns(10);
			}
			{
				textFieldReturnerId = new JTextField();
				textFieldReturnerId.setEditable(false);
				textFieldReturnerId.setText(returnBookDTO.getReturnerId());
				panel.add(textFieldReturnerId, "cell 3 4,alignx left");
				textFieldReturnerId.setColumns(10);
			}

			btnBack.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					dispose();
				}
			});
			panel.add(btnBack, "cell 4 9 2 1,growx");

			{
				labelMessage = new JLabel("     ");
				labelMessage.setForeground(Color.BLUE);
				panel.add(labelMessage, "cell 0 10 6 1");
			}
			{
				textFieldUsername = new JTextField();
				textFieldUsername.setEditable(false);
				textFieldUsername.setText(logonDTO.getUserName());
				panel.add(textFieldUsername, "cell 0 11 2 1,growx");
				textFieldUsername.setColumns(10);
			}
			{
				textFieldLevel = new JTextField();
				textFieldLevel.setEditable(false);
				textFieldLevel.setText(logonDTO.getUserTitle());
				panel.add(textFieldLevel, "cell 3 11 3 1,growx");
				textFieldLevel.setColumns(10);
			}
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			getContentPane().add(scrollPane, BorderLayout.CENTER);
			{
				panelResults = new JPanel();
				scrollPane.setViewportView(panelResults);
				drawBottomPanelHeader(panelResults);
			}
		}
	}

	private void drawBottomPanelHeader(JPanel panel) {
		if (panel.getComponentCount() > 0) {
			panel.removeAll();
		}
		panel.setLayout(new MigLayout("", "[][][grow][]", "[][][]"));

	}

	public void reDrawThis(ActionListener viewBookListener,
			ActionListener returnABookListener) {

		panelResults.removeAll();
		int selectedBookIndex = 1;

		JLabel lblDueDate = new JLabel("Due Date");
		panelResults.add(lblDueDate, "cell 0 0 2 1,center");
		JLabel lblcatalogTitle = new JLabel("[Catalog] Title");
		panelResults.add(lblcatalogTitle, "cell 2 0,center");

		for (UserLoanBookDTO loanedBook : loanedBooks) {
			BookDTO bookDTO = loanedBook.getBookDTO();
			String ddc = bookDTO.getDeweyDecimalNo();
			String title = bookDTO.getTitle();

			JLabel label = new JLabel(selectedBookIndex + ".");
			panelResults.add(label, "flowx,cell 0 " + selectedBookIndex + 1);

			String dueDate = dateFormat.format(loanedBook.getDueDate());
			JLabel lblDate = new JLabel(dueDate.toString());
			panelResults.add(lblDate, "cell 1 " + selectedBookIndex + 1);

			JButton btnViewBook = new JButton("[" + ddc + "] " + title);
			btnViewBook.setActionCommand(String.valueOf(selectedBookIndex - 1));
			btnViewBook.addActionListener(viewBookListener);
			btnViewBook.setFont(new Font("Arial", Font.PLAIN, 11));
			panelResults.add(btnViewBook, "cell 2 " + selectedBookIndex + 1
					+ ",growx");

			JButton btnReturn = new JButton("Return Book");
			btnReturn.setActionCommand(String.valueOf(selectedBookIndex - 1));
			btnReturn.addActionListener(returnABookListener);
			panelResults.add(btnReturn, "cell 3 " + selectedBookIndex + 1);

			panelResults.revalidate();
			panelResults.repaint();

			selectedBookIndex++;
		}
	}

	public void displayMessage(String message) {
		labelMessage.setText(message);
	}

}
